package com.kdtech.analyse.common;

import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.utils.StringUtils;

public class AnalyseSelector {
	
	private String url;
	private String urlNodeSelector;
	private String selector;
	private Integer numberOfElements;
	private String attr;
	private List<ProcessRule> ProcessRules = new ArrayList<ProcessRule>();
	private UrlHandler urlHandler;
	
	
	public static AnalyseSelector init() {
		AnalyseSelector jSoupSelector = new AnalyseSelector();
		return jSoupSelector;
	}
	
	public static AnalyseSelector withIframeUrl(String nodeSelector) {
		AnalyseSelector jSoupSelector = new AnalyseSelector();
		jSoupSelector.urlNodeSelector = nodeSelector;
		return jSoupSelector;
	}
	
	public static AnalyseSelector withUrlHandler(UrlHandler urlHandler) {
		AnalyseSelector jSoupSelector = new AnalyseSelector();
		jSoupSelector.urlHandler = urlHandler;
		return jSoupSelector;
	}
	
	
	public AnalyseSelector select(String selector) {
		this.selector = selector;
		return this;
	}
	
	public AnalyseSelector attr(String attr) {
		this.attr = attr;
		return this;
	}
	
	public AnalyseSelector elementAt(Integer numberOfElements){
		this.numberOfElements = numberOfElements;
		return this;
	}	
	
	public AnalyseSelector first(){
		this.numberOfElements = 0;
		return this;
	}
	
	public  AnalyseSelector replace(String param1,String params2){
		ProcessRules.add( new ProcessRule(ProcessType.REPLACE,param1,params2));
		return this;
	}
	public  AnalyseSelector substringBefore(String param1){
		ProcessRules.add( new ProcessRule(ProcessType.SUBSTRING_BEFORE,param1,null));
		return this;
	}
	public  AnalyseSelector substringBeforeLast(String param1){
		ProcessRules.add( new ProcessRule(ProcessType.SUBSTRING_BEFORE_LAST,param1,null));
		return this;
	}
	public  AnalyseSelector substringAfter(String param1){
		ProcessRules.add( new ProcessRule(ProcessType.SUBSTRING_AFTER,param1,null));
		return this;
	}
	public  AnalyseSelector substringAfterLast(String param1){
		ProcessRules.add( new ProcessRule(ProcessType.SUBSTRING_AFTER_LAST,param1,null));
		return this;
	}
	public  AnalyseSelector substringBetween(String param1,String params2){
		ProcessRules.add( new ProcessRule(ProcessType.SUBSTRING_BETWEEN,param1,params2));
		return this;
	}

	public AnalyseSelector() {
	}

	public String getAnalyseString(Document doc,String url) {
		
		if (urlNodeSelector!=null || urlHandler!=null ){
			String updateUrl = null;
			if (urlHandler!=null ){
				updateUrl = urlHandler.getUrl(doc, url);
			}else if (urlNodeSelector!=null){
				Elements select = doc.select(urlNodeSelector);
				updateUrl = select.attr("src");
			}
			
			if (StringUtils.isNullOrEmpty(updateUrl)){
				return null;
			}
			UrlMeta meta = CrawlHTML.responseToURL(updateUrl);
			if (meta==null){
				return null;					
			}
			String htmltxt = meta.getHtml();
			doc = Jsoup.parse(htmltxt);			
		}
		String text = null;
		if (selector==null){
			text = doc.text();
		}else{
			Elements select = doc.select(selector);
			if (numberOfElements!=null){
				Element element = select.get(numberOfElements);
				if (attr!=null){
					text = element.attr(attr);
				}else{				
					text = element.text();
				}
			}else{
				if (attr!=null){
					text = select.attr(attr);
				}else{				
					text = select.text();
				}
			}
		}
		
		
		for (ProcessRule process : ProcessRules) {
			text = process.getString(text);
		}
		return text;
	}

	
	
	
	

}
